package com.wondersgroup.framework.sample.component.data;

import com.wondersgroup.framework.core.bo.Page;
import com.wondersgroup.framework.sample.component.grid.vo.GridVO;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class GridData
{
  private ServletContext context;

  public GridData(HttpServletRequest request)
  {
    this.context = request.getSession().getServletContext();
  }

  public Page getPage(int start, int size) {
    List data = (List)this.context.getAttribute("gridList");
    Page page = new Page();
    List result = new ArrayList();
    int loop = size;

    if (data.size() < loop)
      loop = data.size();

    for (int i = start; i < start + loop; ++i) {
      if (i < data.size())
        result.add(data.get(i));

    }

    page.setCurrentPageNo(start / size + 1);
    page.setCurrentPageSize(size);
    page.setResult(result);
    page.setTotalSize(data.size());

    return page;
  }

  public GridVO getRecord(int id) {
    List data = (List)this.context.getAttribute("gridList");
    return ((GridVO)data.get(id - 1));
  }
}